(function () {
    
    var UpdateRol = function ( container ) {
        
        this.container          = container;
        this.form               = container.find("form");
        this.ids                = [];
        this.menu               = new Menu( container.find("ul.menu") );
        this.modules            = new ModuleList( container ); //;
        //this.checkbox           = container.find("div.form-container label:first-child input[type=checkbox]");
        this.currentModule      = window.location.hash.substring(1);
        
        this.menu.addEventListener(Menu.ON_BUTTON_CLICK, $.proxy(this.onMenuButtonClick, this));
        
        for(var i=0; i < this.modules.modules.length; i++){
            
            if(this.modules.modules[i].id == this.currentModule){
                
                this.showModule(this.currentModule);
                return;
                
            }
            
        }
        console.log("1");
        this.currentModule = "";
        //this.showModule(this.modules.modules[0].id);
        
    }
    UpdateRol.fn = UpdateRol.prototype;
    
    
    UpdateRol.fn.showModule = function ( id ) {
        
        console.log("UpdateRol.fn.showModule - id:"+id);
        
        if(this.currentModule){
            this.modules.hide(this.currentModule);
            this.menu.unselectButton(this.currentModule);
        }
        
        this.modules.show(id);
        this.menu.selectButton(id);
        
        this.changeAnchor(id);
        
        this.currentModule = id;
        
    }
    
    
    UpdateRol.fn.changeAnchor = function ( id ) {
        
        var anchor = "#" + id;
        
        this.form.attr("action", anchor);
        window.location.hash = anchor;
        
    }
    
    
    UpdateRol.fn.onMenuButtonClick = function ( event ) {
        
        console.log("UpdateRol.fn.onMenuButtonClick");
        console.log(event);
        
        this.showModule(event.id);
        
    }
    
    
    
    
    
    
    
    /*
    Menu
    */
    
    var Menu = function ( container ) {
        
        this.container  = container;
        this.buttons    = container.find("a.button");
        
        this.buttons.click($.proxy(this.onButtonClick, this));
        
    }
    
    Menu.fn = Menu.prototype;
    
    EventDispatcher.prototype.apply(Menu.prototype);
    
    
    Menu.fn.validateID = function ( id ) {
        
        return Boolean(this.buttons.filter("[href=#" + id + "]").length);
        
    }
    
    
    Menu.fn.selectButton = function ( id ) {
        
        this.buttons.filter("[href=#" + id + "]").addClass("selected");
        
    }
    
    
    Menu.fn.unselectButton = function ( id ) {
        
        this.buttons.filter("[href=#" + id + "]").removeClass("selected");
        
    }
    
    
    Menu.fn.onButtonClick = function ( event ) {
        
        event.preventDefault();
        
        var a = $(event.currentTarget);
        var id = a.attr("href");
            id = id.substring(1);
        
        this.dispatchEvent({ type:Menu.ON_BUTTON_CLICK, id:id });
        
    }
    Menu.ON_BUTTON_CLICK = "onButtonClick";
    
    UpdateRol.Menu = Menu;
    
    
    
    
    
    
    
    /*
    ModuleList
    */
    
    var ModuleList = function ( container ) {
        
        this.container  = container;
        this.modules    = [];
        
        var modules = container.find("div.form-container");
        var mod;
        
        for(var i=0; i < modules.length; i++){
            
            mod = new Module( $(modules[i]) );
            
            this.modules.push(mod);
            
        }
        
        //console.log(this.modules);
        
    }
    ModuleList.fn = ModuleList.prototype;
    
    
    ModuleList.fn.getModule = function ( id ) {
        
        for(var i=0; i < this.modules.length; i++){
            
            if( this.modules[i].id == id ){
                
                return this.modules[i];
                
            }
            
        }
        
    }
    
    
    ModuleList.fn.show = function ( id ) {
        
        console.log("ModuleList.fn.show - id:"+id);
        var item = this.getModule(id);
            item.container.show();
        
    }
    
    
    ModuleList.fn.hide = function ( id ) {
        
        var item = this.getModule(id);
            item.container.hide();
        
    }
    
    ModuleList.fn = ModuleList.prototype;
    
    UpdateRol.ModuleList = ModuleList;
    
    
    
    
    
    
    
    /*
    Module
    */
    
    var Module = function ( container ) {
        
        this.container          = container;
        this.id                 = container.attr("id");
        this.masterCheckbox     = container.find("input[data-master]");
        this.commonCheckboxes   = container.find("input[type=checkbox]:not([data-master])");
        //console.log(this.checkboxes);
        var checkbox;
        var mod;
        /*
        for(var i=0; i < this.firstCheckbox.length; i++){
            
            checkbox = $(this.firstCheckbox[i]);
            
            if(!checkbox.is(":checked")){
                
                console.log("caca");
                
            };
            
        }
        */
        this.masterCheckbox.change($.proxy(this.onMasterCheckboxChange, this));
        this.commonCheckboxes.change($.proxy(this.onCommonCheckboxChange, this));
        
    }
    
    Module.fn = Module.prototype;
    
    EventDispatcher.prototype.apply(Module.prototype);
    
    UpdateRol.ModuleList.Module = Module;
    
    
    Module.fn.active = function () {
        
        //this.checkboxes.removeAttr("disabled");
        
    }
    
    
    Module.fn.desactive = function () {
        
        //this.checkboxes.attr("disabled", "true");
        this.commonCheckboxes.removeAttr("checked");
        
    }
    
    
    Module.fn.onMasterCheckboxChange = function ( event ) {
        
        console.log("Module.fn.onMasterCheckboxChange");
        var checkbox    = $(event.currentTarget);
        //var mod         = checkbox.parent().parent().parent().parent();
        
        //console.log(mod);
        //console.log(checkbox.is(":checked"));
        if(!checkbox.is(":checked")){
            
            this.commonCheckboxes.removeAttr("checked");
            
        }
        //this.dispatchEvent(
        
    }
    
    
    Module.fn.onCommonCheckboxChange = function ( event ) {
        
        console.log("Module.fn.onCommonCheckboxChange");
        console.log(this.masterCheckbox);
        this.masterCheckbox.prop("checked", true);
        
    }
    
    
    $(document).ready(function(){

        new UpdateRol( $("#update-rol") );

    });
    
})();